package com.licenta.fbApp.server;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.jiql.jdbc.Driver;

public class DB_Connect {
	
	private static Connection conn; 
	private static Statement smt;
	
	public DB_Connect()
	{	
		

	String url = "jdbc:jiql://89.36.26.247:3306/iuliabejan";
	try
	{
		conn = null;
		Properties props = new Properties();
		props.put("user","iuliabejan");
		props.put("password","bYtewyvmhuSAbvBv");
		
		@SuppressWarnings("rawtypes")
		Class clazz = Class.forName("org.jiql.jdbc.Driver");
		Driver driver = (Driver) clazz.newInstance();
		conn = driver.connect(url,props);
		System.out.println("connected jiql ?" + (conn != null));
		for (int i = 0; i < 10; i++) {
				if ( conn == null ) {				
						conn = driver.connect(url,props);				
				}
				else {
					System.out.println("... connection established ....");
					break;
				}
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		
		try {
			smt = conn.createStatement();
			System.out.println("statement created...");
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		}		
	}
	
	public boolean update( String query) 
	{
		try {
			smt.executeUpdate(query);
			return true;
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			return false;
		}
	}
	
	public ResultSet execute ( String query )
	{
		try {
			smt.execute( query );
			conn.commit();
			return smt.getResultSet();
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	public void close()
	{
		try {
			smt.close();		
			conn.close();
		} catch (SQLException e) {
			System.err.println(e.getMessage());
		}
	}
}
